package com.jhanlos.flights.worldwind.geom;

import java.awt.Color;
import java.awt.Font;
import java.awt.Point;

import javax.media.opengl.GL;


import com.jhanlos.flights.worldwind.texture.TextureUtils;
import com.sun.opengl.util.texture.Texture;
import gov.nasa.worldwind.awt.WorldWindowGLCanvas;
import gov.nasa.worldwind.geom.Angle;
import gov.nasa.worldwind.geom.LatLon;
import gov.nasa.worldwind.geom.Position;
import gov.nasa.worldwind.geom.Vec4;
import gov.nasa.worldwind.globes.Globe;
import gov.nasa.worldwind.render.AnnotationAttributes;
import gov.nasa.worldwind.render.DrawContext;
import gov.nasa.worldwind.render.FrameFactory;
import gov.nasa.worldwind.render.GlobeAnnotation;
import gov.nasa.worldwind.render.MultiLineTextRenderer;
import gov.nasa.worldwind.render.Renderable;
import gov.nasa.worldwind.render.SurfaceCircle;
import gov.nasa.worldwind.util.Logging;

public class AirportObject implements Renderable {

	private Vec4 center;
	private float airSize;
	private String name;
	
	private double lastLat;
    private double lastLon;
    private double lastAlt;
    private Color objectColor;
    private Angle myOrientation = Angle.fromDegrees(0);
    private Angle myElevation = Angle.fromDegrees(90);
    private Globe myGlobe;  // make sure this is initalized before passed in
    
    Cone cone;
    
    
 // cube data for body of sat
    private float[][] cubeData = { {-0.5f, -0.5f, 1.5f}, 
  		  						 {0.5f, -0.5f, 01.5f},
  		  						 {0.5f, 0.5f, 01.5f}, 
  		  						 {-0.5f, 0.5f, 01.5f}, 
  		  						 {-0.5f, -0.5f, -0.0f},
  		  						 {0.5f, -0.5f, -0.0f}, 
  		  						 {0.5f, 0.5f, -0.0f}, 
  		  						 {-0.5f, 0.5f, -0.0f}};
    private float[][] cubeData2 = { {(float) (-Math.sqrt(2.0)*0.5f), 0.0f, 1.5f}, 
									 {0.0f, (float) (-Math.sqrt(2.0)*0.5f), 01.5f},
									 {(float) (Math.sqrt(2.0)*0.5f), 0.0f, 01.5f}, 
									 {0.0f, (float) (Math.sqrt(2.0)*0.5f), 01.5f}, 
									 {(float) (-Math.sqrt(2.0)*0.5f), 0.0f, 0.0f}, 
									 {0.0f, (float) (-Math.sqrt(2.0)*0.5f), 0.0f},
									 {(float) (Math.sqrt(2.0)*0.5f), 0.0f, 0.0f}, 
									 {0.0f, (float) (Math.sqrt(2.0)*0.5f), 0.0f}};
    
    private Texture airportP, tower,tower2;
    
    int circleViewTransparency = 100; //0-255
    
    public AirportObject(double centerLatRad, double centerLonRad, double centerAltitude, float airSize,String name, Globe globe)
    {
    	this.name = name;
        this.myGlobe = globe;
        this.airSize = airSize;
        center = myGlobe.computePointFromPosition(
                    Angle.fromRadiansLatitude(centerLatRad), 
                    Angle.fromRadiansLongitude(centerLonRad), centerAltitude);
        
        
        
        this.lastLat = centerLatRad;
        this.lastLon = centerLonRad;
        this.lastAlt = centerAltitude;

        Color transGreen = new Color(0.0f,0.0f,1.0f,circleViewTransparency/255.0f);
        cone = new Cone(myGlobe, centerLatRad, centerLonRad, 250, 450,myOrientation,myElevation, transGreen);
        
    } 
    
    public void setName(String name) {
		this.name = name;
	}
    
    public void setLastAlt(double lastAlt) {
		this.lastAlt = lastAlt;
		center = myGlobe.computePointFromPosition(
                Angle.fromRadiansLatitude(lastLat), 
                Angle.fromRadiansLongitude(lastLon), lastAlt);
    
	}
    
    public void setLastLon(double lastLon) {
		this.lastLon = lastLon;
		center = myGlobe.computePointFromPosition(
                Angle.fromRadiansLatitude(lastLat), 
                Angle.fromRadiansLongitude(lastLon), lastAlt);
    
	}
    public void setLastLat(double lastLat) {
		this.lastLat = lastLat;
		center = myGlobe.computePointFromPosition(
                Angle.fromRadiansLatitude(lastLat), 
                Angle.fromRadiansLongitude(lastLon), lastAlt);
    
	}
    
    @Override
	public void render(DrawContext dc) {
    	if (dc == null)
        {
            String msg = Logging.getMessage("nullValue.DrawContextIsNull");
            Logging.logger().severe(msg);
            throw new IllegalArgumentException(msg);
        }
        
        if(tower == null)
        {
            // load textures
            
            airportP = TextureUtils.loadTexture("textures/pista.png");
            tower = TextureUtils.loadTexture("textures/tower.png");
            tower2 = TextureUtils.loadTexture("textures/tower.png");
        }

        javax.media.opengl.GL gl = dc.getGL();
        
        gl.glEnable(GL.GL_TEXTURE_2D);
        
        gl.glPushAttrib(javax.media.opengl.GL.GL_TEXTURE_BIT | javax.media.opengl.GL.GL_ENABLE_BIT | javax.media.opengl.GL.GL_CURRENT_BIT);
        
        
        gl.glMatrixMode(javax.media.opengl.GL.GL_MODELVIEW);
        
        gl.glPushMatrix();
        
        
        gl.glColor3d( 1.0 , 1.0 , 1.0 ); // white

        Position p = myGlobe.computePositionFromPoint(center);
       // set center
       gl.glTranslated(this.center.x, this.center.y, this.center.z);
       
       dc.getGL().glRotated(90 + p.getLongitude().getDegrees(), 0, 1, 0);
       dc.getGL().glRotated(myOrientation.getDegrees(), -1, 0, 0);
       dc.getGL().glRotated(p.getLatitude().getDegrees() * myOrientation.sin(), 0, 1, 0);
       dc.getGL().glRotated(myElevation.getDegrees(), 0, -1, 0);
       dc.getGL().glRotated(p.getLatitude().getDegrees() * myElevation.sin(), -1, 0, 0);

      
        drawTower(gl, airSize);
        
        drawAirstrip(gl, airSize);
        
        gl.glPopMatrix();
        

        gl.glPopAttrib();
        
        gl.glDisable(GL.GL_TEXTURE_2D);
        
        AnnotationAttributes geoAttr = createFontAttribs(new Color(1.0f,0,0));
        GlobeAnnotation an = new GlobeAnnotation(name, Position.fromRadians(lastLat, lastLon, lastAlt), geoAttr);
        an.render(dc);
        cone.setVertexPosition(center.x, center.y, center.z);
        //cone.setColor(objectColor);   
        cone.render(dc);
        
        
	}
	
    private AnnotationAttributes createFontAttribs(Color textColor)
    {
        AnnotationAttributes geoAttr = new AnnotationAttributes();
            geoAttr.setFrameShape(FrameFactory.SHAPE_NONE);  // No frame
            geoAttr.setFont(Font.decode("Arial-ITALIC-12"));
            geoAttr.setTextColor(textColor);
            geoAttr.setTextAlign(MultiLineTextRenderer.ALIGN_CENTER);
            geoAttr.setDrawOffset(new Point(0, 5)); // centered just above
            geoAttr.setEffect(MultiLineTextRenderer.EFFECT_OUTLINE);  // Black outline
            geoAttr.setBackgroundColor(Color.BLACK);
            
            return geoAttr;
    }
    
    
    private void drawAirstrip(GL gl, float airSize) {
		
    	airportP.bind();
        float sq2 = (float) Math.sqrt(2);
        gl.glBegin(GL.GL_QUADS);
            gl.glNormal3f(0,0,1.0f);
            gl.glTexCoord2f(0f,0f);
            gl.glVertex3f(  (3.5f*sq2*airSize),  (-7.5f*sq2*airSize), 0f );
            gl.glTexCoord2f(1f,0f);
            gl.glVertex3f(  (4f*sq2*airSize),   (-7f*sq2*airSize), 0f );
            gl.glTexCoord2f(1f,1f);
            gl.glVertex3f(  (-7f*sq2*airSize),   (4f*sq2*airSize), 0f );
            gl.glTexCoord2f(0f,1f);
            gl.glVertex3f(  (-7.5f*sq2*airSize),  (3.5f*sq2*airSize), 0f );
        gl.glEnd();
       
	}
    
    private void drawTower(GL gl, float airSize)
    {
    	tower.bind();
        
        gl.glBegin(GL.GL_QUADS);
        // Front face
        gl.glNormal3f(0,0,1.0f);
        gl.glTexCoord2d(0,0);
        //gl.glVertex3fv(cubeData[0],0);
        gl.glVertex3f(cubeData[0][0]*airSize,cubeData[0][1]*airSize,cubeData[0][2]*airSize);
        gl.glTexCoord2d(1,0);
        //gl.glVertex3fv(cubeData[1],0);
        gl.glVertex3f(cubeData[1][0]*airSize,cubeData[1][1]*airSize,cubeData[1][2]*airSize);
        gl.glTexCoord2d(1,1);
        //gl.glVertex3fv(cubeData[2],0);
        gl.glVertex3f(cubeData[2][0]*airSize,cubeData[2][1]*airSize,cubeData[2][2]*airSize);
        gl.glTexCoord2d(0,1);
        //gl.glVertex3fv(cubeData[3],0);
        gl.glVertex3f(cubeData[3][0]*airSize,cubeData[3][1]*airSize,cubeData[3][2]*airSize);
        // Back face
        gl.glNormal3f(0,0,-1.0f);
        gl.glTexCoord2d(0,0);
        //gl.glVertex3fv(cubeData[5],0);
        gl.glVertex3f(cubeData[5][0]*airSize,cubeData[5][1]*airSize,cubeData[5][2]*airSize);
        gl.glTexCoord2d(1,0);
        //gl.glVertex3fv(cubeData[4],0);
        gl.glVertex3f(cubeData[4][0]*airSize,cubeData[4][1]*airSize,cubeData[4][2]*airSize);
        gl.glTexCoord2d(1,1);
        //gl.glVertex3fv(cubeData[7],0);
        gl.glVertex3f(cubeData[7][0]*airSize,cubeData[7][1]*airSize,cubeData[7][2]*airSize);
        gl.glTexCoord2d(0,1);
        //gl.glVertex3fv(cubeData[6],0);
        gl.glVertex3f(cubeData[6][0]*airSize,cubeData[6][1]*airSize,cubeData[6][2]*airSize);
        // Top face
        gl.glNormal3f(0,1.0f,0.0f);
        gl.glTexCoord2d(0,0);
        //gl.glVertex3fv(cubeData[3],0);
        gl.glVertex3f(cubeData[3][0]*airSize,cubeData[3][1]*airSize,cubeData[3][2]*airSize);
        gl.glTexCoord2d(1,0);
        //gl.glVertex3fv(cubeData[2],0);
        gl.glVertex3f(cubeData[2][0]*airSize,cubeData[2][1]*airSize,cubeData[2][2]*airSize);
        gl.glTexCoord2d(1,1);
        //gl.glVertex3fv(cubeData[6],0);
        gl.glVertex3f(cubeData[6][0]*airSize,cubeData[6][1]*airSize,cubeData[6][2]*airSize);
        gl.glTexCoord2d(0,1);
        //gl.glVertex3fv(cubeData[7],0);
        gl.glVertex3f(cubeData[7][0]*airSize,cubeData[7][1]*airSize,cubeData[7][2]*airSize);
//		gl.glEnd();
//
//
//		gl.glBegin(GL.GL_QUADS);
        // Left face
        gl.glNormal3f(-1.0f, 0.0f,0.0f);
        gl.glTexCoord2d(0,0);
        //gl.glVertex3fv(cubeData[4],0);
        gl.glVertex3f(cubeData[4][0]*airSize,cubeData[4][1]*airSize,cubeData[4][2]*airSize);
        gl.glTexCoord2d(1,0);
        //gl.glVertex3fv(cubeData[0],0);
        gl.glVertex3f(cubeData[0][0]*airSize,cubeData[0][1]*airSize,cubeData[0][2]*airSize);
        gl.glTexCoord2d(1,1);
        //gl.glVertex3fv(cubeData[3],0);
        gl.glVertex3f(cubeData[3][0]*airSize,cubeData[3][1]*airSize,cubeData[3][2]*airSize);
        gl.glTexCoord2d(0,1);
        //gl.glVertex3fv(cubeData[7],0);
        gl.glVertex3f(cubeData[7][0]*airSize,cubeData[7][1]*airSize,cubeData[7][2]*airSize);
        // Right face
        gl.glNormal3f(1.0f,0.0f,0.0f);
        gl.glTexCoord2d(0,0);
        //gl.glVertex3fv(cubeData[1],0);
        gl.glVertex3f(cubeData[1][0]*airSize,cubeData[1][1]*airSize,cubeData[1][2]*airSize);
        gl.glTexCoord2d(1,0);
        //gl.glVertex3fv(cubeData[5],0);
        gl.glVertex3f(cubeData[5][0]*airSize,cubeData[5][1]*airSize,cubeData[5][2]*airSize);
        gl.glTexCoord2d(1,1);
        //gl.glVertex3fv(cubeData[6],0);
        gl.glVertex3f(cubeData[6][0]*airSize,cubeData[6][1]*airSize,cubeData[6][2]*airSize);
        gl.glTexCoord2d(0,1);
        //gl.glVertex3fv(cubeData[2],0);
        gl.glVertex3f(cubeData[2][0]*airSize,cubeData[2][1]*airSize,cubeData[2][2]*airSize);
        // Bottom face
        gl.glNormal3f(0,-1.0f,0.0f);
        gl.glTexCoord2d(0,0);
        //gl.glVertex3fv(cubeData[4],0);
        gl.glVertex3f(cubeData[4][0]*airSize,cubeData[4][1]*airSize,cubeData[4][2]*airSize);
        gl.glTexCoord2d(1,0);
        //gl.glVertex3fv(cubeData[5],0);
        gl.glVertex3f(cubeData[5][0]*airSize,cubeData[5][1]*airSize,cubeData[5][2]*airSize);
        gl.glTexCoord2d(1,1);
        //gl.glVertex3fv(cubeData[1],0);
        gl.glVertex3f(cubeData[1][0]*airSize,cubeData[1][1]*airSize,cubeData[1][2]*airSize);
        gl.glTexCoord2d(0,1);
        //gl.glVertex3fv(cubeData[0],0);
        gl.glVertex3f(cubeData[0][0]*airSize,cubeData[0][1]*airSize,cubeData[0][2]*airSize);
        gl.glEnd();
        
        
        tower2.bind();
        
        gl.glBegin(GL.GL_QUADS);
        // Front face
        gl.glNormal3f(0,0,1.0f);
        gl.glTexCoord2d(0,0);
        //gl.glVertex3fv(cubeData[0],0);
        gl.glVertex3f(cubeData2[0][0]*airSize,cubeData2[0][1]*airSize,cubeData2[0][2]*airSize);
        gl.glTexCoord2d(1,0);
        //gl.glVertex3fv(cubeData2[1],0);
        gl.glVertex3f(cubeData2[1][0]*airSize,cubeData2[1][1]*airSize,cubeData2[1][2]*airSize);
        gl.glTexCoord2d(1,1);
        //gl.glVertex3fv(cubeData2[2],0);
        gl.glVertex3f(cubeData2[2][0]*airSize,cubeData2[2][1]*airSize,cubeData2[2][2]*airSize);
        gl.glTexCoord2d(0,1);
        //gl.glVertex3fv(cubeData2[3],0);
        gl.glVertex3f(cubeData2[3][0]*airSize,cubeData2[3][1]*airSize,cubeData2[3][2]*airSize);
        // Back face
        gl.glNormal3f(0,0,-1.0f);
        gl.glTexCoord2d(0,0);
        //gl.glVertex3fv(cubeData2[5],0);
        gl.glVertex3f(cubeData2[5][0]*airSize,cubeData2[5][1]*airSize,cubeData2[5][2]*airSize);
        gl.glTexCoord2d(1,0);
        //gl.glVertex3fv(cubeData2[4],0);
        gl.glVertex3f(cubeData2[4][0]*airSize,cubeData2[4][1]*airSize,cubeData2[4][2]*airSize);
        gl.glTexCoord2d(1,1);
        //gl.glVertex3fv(cubeData2[7],0);
        gl.glVertex3f(cubeData2[7][0]*airSize,cubeData2[7][1]*airSize,cubeData2[7][2]*airSize);
        gl.glTexCoord2d(0,1);
        //gl.glVertex3fv(cubeData2[6],0);
        gl.glVertex3f(cubeData2[6][0]*airSize,cubeData2[6][1]*airSize,cubeData2[6][2]*airSize);
        // Top face
        gl.glNormal3f(0,1.0f,0.0f);
        gl.glTexCoord2d(0,0);
        //gl.glVertex3fv(cubeData2[3],0);
        gl.glVertex3f(cubeData2[3][0]*airSize,cubeData2[3][1]*airSize,cubeData2[3][2]*airSize);
        gl.glTexCoord2d(1,0);
        //gl.glVertex3fv(cubeData2[2],0);
        gl.glVertex3f(cubeData2[2][0]*airSize,cubeData2[2][1]*airSize,cubeData2[2][2]*airSize);
        gl.glTexCoord2d(1,1);
        //gl.glVertex3fv(cubeData2[6],0);
        gl.glVertex3f(cubeData2[6][0]*airSize,cubeData2[6][1]*airSize,cubeData2[6][2]*airSize);
        gl.glTexCoord2d(0,1);
        //gl.glVertex3fv(cubeData2[7],0);
        gl.glVertex3f(cubeData2[7][0]*airSize,cubeData2[7][1]*airSize,cubeData2[7][2]*airSize);
//		gl.glEnd();
//
//
//		gl.glBegin(GL.GL_QUADS);
        // Left face
        gl.glNormal3f(-1.0f, 0.0f,0.0f);
        gl.glTexCoord2d(0,0);
        //gl.glVertex3fv(cubeData2[4],0);
        gl.glVertex3f(cubeData2[4][0]*airSize,cubeData2[4][1]*airSize,cubeData2[4][2]*airSize);
        gl.glTexCoord2d(1,0);
        //gl.glVertex3fv(cubeData2[0],0);
        gl.glVertex3f(cubeData2[0][0]*airSize,cubeData2[0][1]*airSize,cubeData2[0][2]*airSize);
        gl.glTexCoord2d(1,1);
        //gl.glVertex3fv(cubeData2[3],0);
        gl.glVertex3f(cubeData2[3][0]*airSize,cubeData2[3][1]*airSize,cubeData2[3][2]*airSize);
        gl.glTexCoord2d(0,1);
        //gl.glVertex3fv(cubeData2[7],0);
        gl.glVertex3f(cubeData2[7][0]*airSize,cubeData2[7][1]*airSize,cubeData2[7][2]*airSize);
        // Right face
        gl.glNormal3f(1.0f,0.0f,0.0f);
        gl.glTexCoord2d(0,0);
        //gl.glVertex3fv(cubeData2[1],0);
        gl.glVertex3f(cubeData2[1][0]*airSize,cubeData2[1][1]*airSize,cubeData2[1][2]*airSize);
        gl.glTexCoord2d(1,0);
        //gl.glVertex3fv(cubeData2[5],0);
        gl.glVertex3f(cubeData2[5][0]*airSize,cubeData2[5][1]*airSize,cubeData2[5][2]*airSize);
        gl.glTexCoord2d(1,1);
        //gl.glVertex3fv(cubeData2[6],0);
        gl.glVertex3f(cubeData2[6][0]*airSize,cubeData2[6][1]*airSize,cubeData2[6][2]*airSize);
        gl.glTexCoord2d(0,1);
        //gl.glVertex3fv(cubeData2[2],0);
        gl.glVertex3f(cubeData2[2][0]*airSize,cubeData2[2][1]*airSize,cubeData2[2][2]*airSize);
        // Bottom face
        gl.glNormal3f(0,-1.0f,0.0f);
        gl.glTexCoord2d(0,0);
        //gl.glVertex3fv(cubeData2[4],0);
        gl.glVertex3f(cubeData2[4][0]*airSize,cubeData2[4][1]*airSize,cubeData2[4][2]*airSize);
        gl.glTexCoord2d(1,0);
        //gl.glVertex3fv(cubeData2[5],0);
        gl.glVertex3f(cubeData2[5][0]*airSize,cubeData2[5][1]*airSize,cubeData2[5][2]*airSize);
        gl.glTexCoord2d(1,1);
        //gl.glVertex3fv(cubeData2[1],0);
        gl.glVertex3f(cubeData2[1][0]*airSize,cubeData2[1][1]*airSize,cubeData2[1][2]*airSize);
        gl.glTexCoord2d(0,1);
        //gl.glVertex3fv(cubeData2[0],0);
        gl.glVertex3f(cubeData2[0][0]*airSize,cubeData2[0][1]*airSize,cubeData2[0][2]*airSize);
        gl.glEnd();
    }
	
    public void setObjectColor(Color objectColor) {
		this.objectColor = objectColor;
	}
    
    public Color getObjectColor() {
		return objectColor;
	}
	public double getLastLat()
    {
        return lastLat;
    }

    public double getLastLon()
    {
        return lastLon;
    }

    public double getLastAlt()
    {
        return lastAlt;
    }
    public void setAirSize(float airSize) {
		this.airSize = airSize;
	}
    public float getAirSize() {
		return airSize;
	}
}
